import pygame
import sys
import guanqia
import victory
from game import Game, Level
from bullet import Bullet
import levelsMap
from pygame.locals import *


def game_star():
    pygame.init()
    # , FULLSCREEN | HWSURFACE, 32
    try:
        chuangkou = open("data/窗口大小.txt", "r", encoding="utf-8")
        chuangkou_size = chuangkou.read()
        chuangkou.close()
        print(chuangkou_size)
        if str(chuangkou_size) == "全屏模式":
            Game.window = pygame.display.set_mode((Game.W, Game.H), FULLSCREEN | HWSURFACE, 32)
            Game.quanping = False
        else:
            Game.window = pygame.display.set_mode((Game.W, Game.H))
            Game.quanping = True
    except:
        Game.window = pygame.display.set_mode((Game.W, Game.H))

        Game.quanping = True

    pygame.display.set_caption("游戏零零")

    Game.bg = pygame.image.load("img/背景(1700x900)22.png")
    Background_img_rect = Game.bg.get_rect()

    Game.music = pygame.mixer.Sound("wav/游戏.mp3")
    Game.music.play(-1)

    Game.zixinag_img = pygame.image.load("img/纸箱复制.png")

    FPS = 60
    clock = pygame.time.Clock()

    f = open("data/level.txt", "r")
    level_list = f.read()
    print(level_list)
    f.close()
    if int(level_list) == 1:
        Level.levels = levelsMap.level1()
    if int(level_list) == 2:
        Level.levels = levelsMap.level2()
    if int(level_list) == 3:
        Level.levels = levelsMap.level3()
    if int(level_list) == 4:
        Level.levels = levelsMap.level4()
    if int(level_list) == 5:
        Level.levels = levelsMap.level5()
    if int(level_list) == 6:
        Level.levels = levelsMap.level6()
    if int(level_list) == 7:
        Level.levels = levelsMap.level7()
    if int(level_list) == 8:
        Level.levels = levelsMap.level8()
    if int(level_list) == 9:
        Level.levels = levelsMap.level9()
    if int(level_list) == 10:
        Level.levels = levelsMap.level10()

    Game.fashe_music = pygame.mixer.Sound("Wav/发射.wav")
    Game.jump_music = pygame.mixer.Sound("wav/跳.wav")
    Game.run = True
    while Game.run:
        Game.index += 1
        clock.tick(FPS)
        game_get()
        Game.window.blit(Game.bg, Background_img_rect)
        Game.wall.wall_display()
        Game.portal.portal_display()

        Game.monster.monster_display()
        try:
            Game.mini_portal1.portal_display()
            Game.mini_portal2.portal_display()

            Game.mini_portal1.portal_hit_player()
            Game.mini_portal2.portal_hit_player()
        except:
            pass
        text()
        if Game.shengli:
            Game.playerGroup.update(Game.index)
            Game.playerGroup.draw(Game.window)
            Game.player1.confine()
            Game.player1.left_right()
            Game.player1.hit_wall()

            Game.playerGroup2.update(Game.index)
            Game.playerGroup2.draw(Game.window)
            Game.player2.confine()
            Game.player2.left_right()
            Game.player2.hit_wall()

        # 子弹绘制
        for bullet in Game.bullet_list:
            bullet.bullet_fire()
            bullet.bullet_display()
            if bullet.Bullet_rect.left <= 0 or bullet.Bullet_rect.left >= Game.W:
                Game.bullet_list.remove(bullet)

        # 重力
        Game.G_speed += Game.G  # 增加垂直速度
        Game.player1.rect.y += Game.G_speed  # 根据垂直速度更新位置

        Game.G_speed2 += Game.G2  # 增加垂直速度
        Game.player2.rect.y += Game.G_speed2  # 根据垂直速度更新位置

        portal_hit_player()
        recur()
        wall_hit_player2()
        wall_hit_player1()
        monster_hit_players()
        pygame.display.update()


def text():
    guanqia_font = pygame.font.Font("Font/font.ttf", 50)
    shuliang_font = pygame.font.Font("Font/font.ttf", 50)

    level_text = guanqia_font.render(Game.level_text, True, (0, 0, 0))
    Game.window.blit(level_text, (1450, 20))

    score_text = shuliang_font.render("宝石: " + str(Game.gem_number), True, (100, 25, 254))
    Game.window.blit(score_text, (20, 10))


def game_get():
    for event in pygame.event.get():
        mouse_x, mouse_y = pygame.mouse.get_pos()
        print(mouse_x, mouse_y)
        if event.type == pygame.QUIT:
            Game.run = False
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                Game.music.stop()
                Game().__init__()
                game_star()

            # Game.player1
            if event.key == pygame.K_w and not Game.direction_W:
                Game.jump_music.play()
                print("w")
                Game.G_speed = -Game.speed
                Game.direction_W = True

            if event.key == pygame.K_a:
                print("a")
                Game.direction_A = True
                Game.current_images = Game.player1.images_left

            if event.key == pygame.K_d:
                print("d")
                Game.direction_D = True
                Game.current_images = Game.player1.images_right

            if event.key == pygame.K_s:
                if len(Game.bullet_list) < 1:
                    Game.fashe_music.play()

                    if Game.direction == "left":
                        bullet_right = Bullet(Game.player1.rect.left, Game.player1.rect.top + 30, "left", 10)
                        Game.bullet_list.append(bullet_right)

                    elif Game.direction == "right":
                        bullet_left = Bullet(Game.player1.rect.left, Game.player1.rect.top + 30, "right", 10)
                        Game.bullet_list.append(bullet_left)

            if event.key == pygame.K_UP and not Game.direction_W2:
                Game.direction_W2 = True
                Game.jump_music.play()
                Game.G_speed2 = -Game.speed2
                Game.direction_2 = True

            if event.key == pygame.K_LEFT:
                Game.direction_A2 = True

            if event.key == pygame.K_RIGHT:
                Game.direction_D2 = True

            if event.key == pygame.K_DOWN:
                Game.fangkuai = True

            if event.key == pygame.K_SPACE:
                Game.kongge = True

        if event.type == pygame.KEYUP:
            # Game.player1
            if event.key == pygame.K_a:
                Game.direction_A = False
                print("松开a")

            if event.key == pygame.K_d:
                Game.direction_D = False
                print("松开d")

            if event.key == pygame.K_LEFT:
                Game.direction_A2 = False

            if event.key == pygame.K_RIGHT:
                Game.direction_D2 = False

            if event.key == pygame.K_DOWN:
                Game.fangkuai = False

            if event.key == pygame.K_SPACE:
                Game.kongge = False

            if event.key == pygame.K_ESCAPE:
                guanqia.Jiemian()
                Game.music.stop()

            if event.key == pygame.K_p:
                chuangkou = open("data/窗口大小.txt", "w", encoding="utf-8")
                if Game.quanping:
                    Game.window = pygame.display.set_mode((Game.W, Game.H), FULLSCREEN | HWSURFACE, 32)
                    chuangkou.write("全屏模式")
                    chuangkou.close()
                    Game.quanping = False
                else:
                    Game.window = pygame.display.set_mode((Game.W, Game.H))
                    # 设置窗口显示位置
                    Game.quanping = True
                    chuangkou.write("窗口模式")
                    chuangkou.close()


def recur():
    if Game.player1.rect.y >= Game.H or Game.player2.rect.y >= Game.H:
        Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
        Game.sisheng.play()
        Game.music.stop()
        Game().__init__()
        game_star()


def wall_hit_player1():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)
            if Game.wall_list[row][col] in (4, 2, 1):
                if Game.player1.rect.colliderect(wall_rect):
                    Game.player1.rect.y = wall_rect.y - Game.player1.rect.height
                    Game.G_speed = 0  # 碰撞后重置垂直速度
                    Game.direction_W = False

            if Game.wall_list[row][col] in (1, 2):
                for bullet in Game.bullet_list:
                    if bullet.Bullet_rect.colliderect(wall_rect):
                        Game.bullet_list.remove(bullet)
                        pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
                        pengzhuang_music.play()

            if Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player1.rect.colliderect(wall_rect):
                    Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
                    Game.sisheng.play()
                    Game.music.stop()
                    Game().__init__()
                    game_star()

            if Game.wall_list[row][col] == 4:
                for bullet in Game.bullet_list:
                    if bullet.Bullet_rect.colliderect(wall_rect):
                        Game.bullet_list.remove(bullet)
                        Game.wall_list[row][col] = 0
                        pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
                        pengzhuang_music.play()

            if Game.wall_list[row][col] == 5:
                if Game.player1.rect.colliderect(wall_rect):
                    Game.wall_list[row][col] = 0
                    chibaoshi_music = pygame.mixer.Sound("wav/吃宝石.wav")
                    chibaoshi_music.play()
                    Game.gem_number -= 1


def wall_hit_player2():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)
            if Game.wall_list[row][col] in (1, 2, 4):
                if Game.player2.rect.colliderect(wall_rect):
                    Game.player2.rect.y = wall_rect.y - Game.player2.rect.height
                    Game.G_speed2 = 0  # 碰撞后重置垂直速度
                    Game.direction_W2 = False

            if Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player2.rect.colliderect(wall_rect):
                    Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
                    Game.sisheng.play()
                    Game.music.stop()
                    Game().__init__()
                    game_star()

            if Game.wall_list[row][col] == 0:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.direction2 == "right":
                    try:
                        if Game.player2.rect.colliderect(wall_rect):
                            if Game.player2.rect.left <= wall_rect.left:
                                Game.window.blit(Game.zixinag_img, (wall_rect.x + 50, wall_rect.y - 30))
                    except:
                        pass
                if Game.direction2 == "left":
                    try:
                        if Game.player2.rect.colliderect(wall_rect):
                            if Game.player2.rect.right >= wall_rect.right:
                                Game.window.blit(Game.zixinag_img, (wall_rect.x - 50, wall_rect.y - 30))
                    except:
                        pass
                if Game.player2.rect.colliderect(wall_rect) and Game.fangkuai and Game.direction2 == "right":
                    try:
                        if Game.wall_list[row + 1][col + 2] == 0:
                            Game.wall_list[row + 1][col + 2] = 4
                        Game.fangkuai = False
                    except:
                        Game.fangkuai = False
                if Game.player2.rect.colliderect(wall_rect) and Game.fangkuai and Game.direction2 == "left":
                    try:
                        if Game.wall_list[row + 1][col - 1] == 0:
                            Game.wall_list[row + 1][col - 1] = 4
                        Game.fangkuai = False
                    except:
                        Game.fangkuai = False

            if Game.wall_list[row][col] == 6:
                if Game.player2.rect.colliderect(wall_rect):
                    Game.wall_list[row][col] = 0
                    chibaoshi_music = pygame.mixer.Sound("wav/吃宝石.wav")
                    chibaoshi_music.play()
                    Game.gem_number -= 1


def monster_hit_players():
    for monster in Game.monster_list:
        monster_rect = Game.monster.monster_image.get_rect()
        monster_rect.x = monster["x"]
        monster_rect.y = monster["y"]
        if Game.player1.rect.colliderect(monster_rect) or Game.player2.rect.colliderect(
                monster_rect):
            Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
            Game.sisheng.play()
            pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
            pengzhuang_music.play()
            Game.music.stop()
            Game().__init__()
            game_star()


def portal_hit_player():
    if Game.portal.Portal_rect.colliderect(Game.player1.rect) and \
            Game.portal.Portal_rect.colliderect(Game.player2.rect):
        if Game.gem_number <= 0:
            font = pygame.font.Font("Font/font.ttf", 220)
            score_text = font.render(" 恭喜您成功通过 ", True, (8, 25, 15), (255, 85, 0))
            score_text2 = font.render(" 请点击空格下一关 ", True, (8, 25, 15), (255, 85, 0))
            Game.window.blit(score_text, (50, 200))
            Game.window.blit(score_text2, (50, 450))
            print("已通过")
            Game.shengli = False
            Game.music.stop()
            Game().__init__()
            victory.Jiemian()

        else:
            font3 = pygame.font.Font("Font/font.ttf", 50)
            score_text = font3.render(f" 还有{Game.gem_number}个宝石没收 ", True, (8, 25, 15), (255, 85, 0))
            Game.window.blit(score_text, (600, 820))
            print(f" 还有{Game.gem_number}个宝石没收 ")

    elif Game.portal.Portal_rect.colliderect(Game.player1.rect) or \
            Game.portal.Portal_rect.colliderect(Game.player2.rect):

        font3 = pygame.font.Font("Font/font.ttf", 50)
        score_text = font3.render(f" 英雄们没齐，还有{Game.gem_number}个宝石没收 ", True, (8, 25, 15), (255, 85, 0))
        Game.window.blit(score_text, (600, 820))
        print(f" 还有{Game.gem_number}个宝石没收 ")


if __name__ == '__main__':
    Game().__init__()
    game_star()
